package com.hanxiaozhang;

import com.hanxiaozhang.util.DistributedLockUtil;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.experimental.Accessors;

import java.util.HashMap;
import java.util.Map;

/**
 * 〈一句话功能简述〉<br>
 * 〈配置信息类〉
 *
 * @author hanxinghua
 * @create 2022/9/3
 * @since 1.0.0
 */
@Data
@Accessors(chain = true)
public class Configuration {

    public Configuration() {

    }

    // ------ zk ------
    /**
     * zk地址
     */
    private String zkAddress;

    /**
     * zk锁地址前缀
     */
    private String zkLockPathPrefix;

    /**
     * zk锁地址
     */
    private String zkLockProjectPath;

    /**
     * zk清除无效节点开关
     */
    private Boolean zkClearInvalidNode = true;

    /**
     * zk清除无效节点频率，单位秒
     */
    private Integer zkClearInvalidNodeRate;

    /**
     * zk清除无效节点开始时间，单位秒
     */
    private Integer zkClearInvalidNodeBeforeTime;


    // ------ redis ------
    /**
     * Redis模式
     */
    private String redisModel;

    /**
     * Redis地址
     */
    private String redisAddress;

    /**
     * Redis密码
     */
    private String redisPassword;

    /**
     * Redis数据库（database）
     */
    private Integer redisDb;

    /**
     * Redis主节点地址
     */
    private String redisMasterAddress;

    /**
     * Redis从节点地址
     */
    private String redisSlaveAddresses;

    /**
     * Redis Master名称
     */
    private String redisMasterName;

    /**
     * Redis哨兵地址
     */
    private String redisSentinelAddress;

    /**
     * 其他配置信息
     */
    private Map<String, String> properties = new HashMap<>();


    /**
     * 获取zk基础锁路径
     *
     * @return
     */
    public String getZkBaseLockPath() {

        String path = "";
        if (!getZkLockPathPrefix().startsWith(DistributedLockUtil.SEPARATOR)) {
            path = DistributedLockUtil.SEPARATOR + getZkLockPathPrefix();
        }else {
            path = getZkLockPathPrefix();
        }
        if (!getZkLockProjectPath().startsWith(DistributedLockUtil.SEPARATOR)) {
            path += DistributedLockUtil.SEPARATOR + getZkLockProjectPath();
        } else {
            path += getZkLockProjectPath();
        }
        if (!path.endsWith(DistributedLockUtil.SEPARATOR)) {
            path += DistributedLockUtil.SEPARATOR;
        }
        return path;
    }


    /**
     * 获取ZkLockPath
     *
     * @param lockKey
     * @return
     */
    public String getZkLockPath(String lockKey) {

        return getZkBaseLockPath() + lockKey;
    }

}
